﻿@model Citi.ReportManager.WebUi.Models.ReportModel
<script type="text/javascript">
    function saveFavorite(categoryId) {
        $.ajax({
            url: '@Url.Action("SaveFavorite")',
            data: { categoryId: categoryId },
            type: "post",
            success: function () {
                var favorite = $("#favorite" + categoryId);

                if (favorite.hasClass("favoriteActive")) {
                    favorite.removeClass("favoriteActive");
                    favorite.addClass("favoriteInactive");
                }
                else {
                    favorite.removeClass("favoriteInactive");
                    favorite.addClass("favoriteActive");
                }
            }
        });
    }
</script>
<div id="divFiles">
    <h2>
        @if (Model.CurrentFolder == null || string.IsNullOrWhiteSpace(Model.CurrentFolder.Path))
        {
            @Model.CurrentCategory.Name;
        }
        else
        {
            @Model.CurrentFolder.Name;
        }</h2>
    <br />
    <div id="divBreadCrumb">
        @for (int i = Model.BreadCrumbs.Count - 1; i >= 0; i--)
        {
            var item = Model.BreadCrumbs[i];

            if (item.Name == Model.CurrentCategory.Name && (Model.CurrentFolder == null || string.IsNullOrWhiteSpace(Model.CurrentFolder.Path)))
            {
            <text> @item.Name </text>
            }
            else
            {
            @(string.IsNullOrWhiteSpace(item.PathDownload)
                      ? Html.ActionLink(item.Name, "Index", new { categoryId = (int?)item.CategoryId, reportFolderId = item.ReportFolderId })
                      : Html.ActionLink(item.Name, "Index", new { categoryId = item.CategoryId, folder = item.PathDownload, reportFolderId = item.ReportFolderId }))
            }

            if (i > 0)
            {
            <text>&nbsp;>&nbsp;</text>
            }
        }
    </div>
    <br />
    <table class="table table-bordered table-striped">
        <tr>
            <th>
                Name
            </th>
            <th style="width: 60px">
                Size
            </th>
        </tr>
        @if (Model.CurrentCategory.ParentId.HasValue)
        {
            <tr>
                <td>
                    <img alt="Up" src="@Url.Content("~/Content/Images/Files/upArrow.png")" style="width: 24px" />
                    &nbsp;
                    @if (Model.CurrentFolder != null && !string.IsNullOrWhiteSpace(Model.CurrentFolder.Name))
                    {
                        @Html.ActionLink("Up", "Index", new { categoryId = Model.CurrentCategory.Id, reportFolderId = Model.CurrentReportFolderId, folder = Model.CurrentFolder.PreviousFolder ?? null })
                    }
                    else
                    {
                        @Html.ActionLink("Up", "Index", new { categoryId = Model.CurrentCategory.ParentId })
                    }
                </td>
                <td>
                </td>
            </tr>
        }
        @if (!Model.Items.Any())
        {
            <tr>
                <td colspan="2">
                    There's no report in this folder
                </td>
            </tr>
        }
        else
        {
            foreach (var item in Model.Items)
            {
            <tr>
                <td onmouseover="@(item.PathDownload == null
                        ? string.Format("$('#favorite{0}').show();", item.CategoryId)
                        : string.Empty)" onmouseout="@string.Format("$('#favorite{0}').hide();", item.CategoryId)">
                    <img src="@Url.Content(string.Format("~/Content/Images/Files/{0}", item.Icon))" alt="File type" style="width: 24px"/>
                    &nbsp;
                    @if (item.Folder)
                    {                        
                        @Html.ActionLink(item.Name, "Index", new { categoryId = item.CategoryId, folder = item.PathDownload, reportFolderId = item.ReportFolderId })
                        <span id="@string.Concat("favorite", item.CategoryId)" 
                        class="hide @string.Format("favorite{0}ctive", item.CategoryFavorite ? "A" : "Ina")" 
                        onclick = "javascript:saveFavorite(@item.CategoryId);">&nbsp;&nbsp;&nbsp;&nbsp;</span>                       
                    }
                    else
                    {
                        @Html.ActionLink(item.Name, "Download", new { categoryId = item.CategoryId, folder = item.PathDownload })
                    }
                </td>
                @if (!item.Folder)
                {
                    <td>@item.Size.ToString("#0.00") MB
                    </td>
                }
                else
                {
                    <td>
                    </td>
                }
            </tr>
            }
        }
    </table>
    @if (Model.CurrentCategory.ParentId.HasValue)
    {
        <div>
            @if (Model.CurrentFolder != null && !string.IsNullOrWhiteSpace(Model.CurrentFolder.Name))
            {
                @Html.ActionLink("Back", "Index", new { categoryId = Model.CurrentCategory.Id, reportFolderId = Model.CurrentReportFolderId, folder = Model.CurrentFolder.PreviousFolder ?? null })
            }
            else
            {
                @Html.ActionLink("Back", "Index", new { categoryId = Model.CurrentCategory.ParentId })
            }
        </div>
    }
</div>
